<?php
/**
 * 微信车间管理
 */
namespace app\wechat\admin;

use app\common\controller\WechatAdmin;
use app\common\service\OrderUser as OrderUserService;

class Workshop extends WechatAdmin
{

    public function initialize() {
    	parent::initialize();

    }

    /**
     * 车间任务列表
     *
     * @author King
     * @date 2020年8月19日
     *
     */
    public function index(){
    	return $this->assign([])->fetch();
    }

    /**
     * 获取菲码详情
     *
     * @author King
     * @date 2020年8月19日
     *
     */
    public function getQrcodeInfo() {
    	$qrcode = $this->request->param('qrcode/s', '');
    	$items = explode(',', $qrcode);
    	if (count($items) != 6 || $items[0] != 2 || !is_numeric($items[1]) || !is_numeric($items[2]) || !is_numeric($items[3])) {
			return $this->Json(0, '这个二维码是什么鬼，不是菲码哦');
    	} else {
    		$mid = $items[1];
    		$orderUserService = new OrderUserService();
    		$ouInfo = $orderUserService->getInfoById($mid);
    		if($ouInfo) {
				return $this->Json(1, "订单号：{$ouInfo['client_ordernum']}，扎号：{$ouInfo['zhahao']}，件数：{$ouInfo['jianshu']}，码数：{$ouInfo['size_name']}", $ouInfo);
    		} else {
    			return $this->Json(0, '数据异常');
    		}
    	}
    }

    /**
     * 菲码完成
     *
     * @author King
     * @date 2020年8月19日
     *
     */
    public function over() {
    	$mid = $this->request->param('mid/d', 0);
    	$orderUserService = new OrderUserService();
    	$ouInfo = $orderUserService->getInfoById($mid);
    	if (empty($ouInfo)) {
    		return $this->Json(0, '数据异常');
    	}

    	$weChatTemplate = \We::WeChatTemplate(config('wechat.'));
    	try {
    		$data = [
    			'touser' => arrayGet(session('wechat_user'), 'openid'),
    			'template_id' => '8A2YfUq3lIlpF6QUUGbrU6-CUTYdaQBQlM5e9NOVkHE',
    			'url' => '',
    			'topcolor' => '#00ff00',
    			'data' => [
    				'client_ordernum' => ['value' => $ouInfo['client_ordernum'], 'color' => '#009688'],
    				'zhahao' => ['value' => $ouInfo['zhahao'], 'color' => '#5FB878'],
    				'jianshu' => ['value' => $ouInfo['jianshu'], 'color' => '#FF5722'],
    				'size_name' => ['value' => $ouInfo['size_name'], 'color' => '#1E9FFF'],
    				'nick' => ['value' => arrayGet(session('admin_user'), 'nick', ''), 'color' => '#393D49'],
    				'over_time' => ['value' => date('Y-m-d H:i:s'), 'color' => '#393D49'],
    			],
    		];
    		$res = $weChatTemplate->send($data);
    		if ($res['errcode']==0) {
    			return $this->Json(1);
    		} else {
    			throw new \Exception($res['errmsg']);
    		}
    	} catch (\Exception $e) {
    		return $this->Json(0, $e->getMessage());
    	}


    }
}